from app import app,request,basedir,json,url_for,session,g
import os,sys,random,string
from werkzeug import secure_filename
from app.model import db,Product_class,Product_list,Product_sku,Admin_lookbook,Shop_information
from app.controller.tools import usual_save_img,res_json,baseUrl,file_len


#保存分类菜单
@app.route('/api/v1/save_pro_menu',methods=['POST'])
def save_pro_menu():
    name = request.form.get('name')
    parent_id = request.form.get('parent_id')
    db_save = Product_class(name=name,parent_menu=parent_id)
    db.session.add(db_save)
    db.session.commit()
    return 'success'

#返回分类菜单
@app.route('/api/v1/get_class',methods=['POST'])
def get_class():
    result = Product_class.query.all()
    resData = []
    for item in result:
        resData.append({
            'id':item.id,
            'name':item.name,
            'parent_id':item.parent_menu
        })
    return res_json(200,'成功',resData)

#点击分类菜单 获取产品信息
@app.route('/api/v1/get_product',methods=['POST'])
def get_product():
    class_id = request.form.get('class_id')
    resData = []
    if class_id == None:
        result = Product_list.query.all()
        for item in result:
            resData.append({
                'id':item.id,
                'name':item.name,
                'img_url':baseUrl+item.img_url,
                'price':item.price,
                'goods_link':item.goods_link,
                'class_id':item.class_id
            })
    else:
        result_s = Product_class.query.filter_by(id = class_id).first()
        products_s = result_s.products
        for item in products_s:
            resData.append({
                'id':item.id,
                'name':item.name,
                'price':item.price,
                'img_url':baseUrl+item.img_url,
                'goods_link':item.goods_link,
                'class_id':item.class_id
            })
    return res_json(200,'成功',resData)

#保存/修改 产品信息
@app.route('/api/v1/save_product',methods=['POST'])
def save_product():
    product_img = request.files.get('product_img')
    edit_img =  request.files.get('edit_img')
    goods_name = request.form.get('goods_name')
    goods_price = request.form.get('goods_price')
    goods_link = request.form.get('goods_link')
    class_id = request.form.get('class_id')
    product_id = request.form.get('product_id')
    if not product_id == None:
        img_len = file_len(edit_img)
        result = Product_list.query.filter_by(id = product_id).first()
        result.name = goods_name
        result.price = goods_price
        result.class_id = class_id
        result.goods_link = goods_link
        if not img_len == '0':
            print('进入编辑方法')
            img_url_ed = usual_save_img(product_img)
            print(img_url_ed)
            result.img_url = img_url_ed
        db.session.commit()
        return 'success'
    else:
        img_url = usual_save_img(product_img)
        save_data = Product_list(name=goods_name,price=goods_price,class_id=class_id,img_url=img_url,goods_link=goods_link)
        db.session.add(save_data)
        db.session.commit()
        return 'success'

#复制一个产品接口
@app.route('/api/v1/copy_goods',methods=['POST'])
def copy_goods():
    goods_item = request.form.to_dict('good_item')
    save_data = Product_list(name=goods_name,price=goods_price,class_id=class_id,img_url=img_url,goods_link=goods_link)
    return '1'


#分类id获取一级分类及名称接口
@app.route('/api/v1/get_class_name',methods=['POST'])
def get_class_name():
    class_id = request.form.get('classs_id')
    result = Product_class.query.filter_by(id = class_id).first()
    p_class = Product_class.query.filter_by(id = result.parent_menu).first()
    resDate = {
        'p_class_id':result.parent_menu,
        'p_class_name':p_class.name,
        's_class_id':result.id,
        's_class_name':result.name
    }
    return res_json(200,'成功',resDate)




#返回所有产品信息
@app.route('/api/v1/get_all_product',methods=['POST'])
def get_all_product():
    result = Product_list.query.all()
    resData = []
    for item in result:
        resData.append({
            'id':item.id,
            'name':item.name,
            'img_url':baseUrl+item.img_url,
            'price':item.price,
            'goods_link':item.goods_link,
            'class_id':item.class_id
        })
    return res_json(200,'成功',resData)

#删除商品信息
@app.route('/api/v1/del_product',methods=['POST'])
def del_product():
    product_id = request.form.get('product_id')
    result = Product_list.query.filter_by(id=product_id).first()
    db.session.delete(result)
    db.session.commit()
    return 'success'

#删除分类菜单方法
@app.route('/api/v1/del_node',methods=['POST'])
def del_node():
    node_id = request.form.get('class_id')
    result = Product_class.query.filter_by(id = node_id).first()
    db.session.delete(result)
    db.session.commit()
    return 'success'

#获取sku 
@app.route('/api.v1/get_sku',methods=['POST'])
def get_sku():
    result = Product_sku.query.all()
    resData = []
    for itme in result:
        resData.append({
            'id':itme.id,
            'img_url':baseUrl+itme.img_url,
            'product_id':itme.product_id
        })
    return res_json(200,'成功',resData)

#新增sku 
@app.route('/api/v1/add_sku',methods=['POST'])
def add_sku():
    sku_img = request.files.get('sku_img')
    img_url = usual_save_img(sku_img)
    product_id = request.form.get('product_id')
    result = Product_sku(img_url=img_url,product_id=product_id)
    db.session.add(result)
    db.session.commit()
    return 'success'

#删除sku信息
@app.route('/api/v1/del_sku',methods=['POST'])
def del_sku():
    sku_id = request.form.get('sku_id')
    result = Product_sku.query.filter_by(id=sku_id).first()
    db.session.delete(result)
    db.session.commit()
    return 'success'

#店铺信息 保存数据
@app.route('/api/v1/shop_information',methods=['POST'])
def save_information():
    store_img = request.files.get('store_img')
    store_name = request.form.get('store_name')
    store_infor = request.form.get('store_infor')
    imgUrl = usual_save_img(store_img)
    results = Shop_information(img_url=imgUrl,title=store_name,content=store_infor)
    db.session.add(results)
    db.session.commit()
    return 'success',200


#店铺信息 返回数据
@app.route('/api/v1/shop_getData',methods=['POST'])
def shop_getData():
    result = Shop_information.query.all()
    resData = []
    for item in result:
        resData.append({
            'id':item.id,
            'title':item.title,
            'img_url':baseUrl+item.img_url,
            'content':item.content
        })
    return res_json(200,'成功',resData)

#店铺信息 删除
@app.route('/api/v1/shopinfor_del',methods=['POST'])
def shopinfor_getData():
    shop_id = request.form.get('shop_id')
    results = Shop_information.query.filter_by(id=shop_id).first()
    db.session.delete(results)
    db.session.commit()
    return 'success'

